{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 列表"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 列表的常用操作"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.取值和索引"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wangwu\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"zhangsan\", \"lisi\", \"wangwu\"]\n",
    "\n",
    "print(name_list[2])\n",
    "print(name_list.index(\"wangwu\"))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.修改"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['zhangsan', '李四', 'wangwu']\n"
     ]
    }
   ],
   "source": [
    "name_list[1] = \"李四\"\n",
    "\n",
    "print(name_list)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.增加"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['zhangsan', '小妹妹', '李四', 'wangwu', '王小二']\n",
      "['zhangsan', '小妹妹', '李四', 'wangwu', '王小二', '孙悟空', '猪二哥', '沙师弟']\n"
     ]
    }
   ],
   "source": [
    "# append 方法可以向列表的末尾追加数据\n",
    "name_list.append(\"王小二\")\n",
    "\n",
    "# insert 方法可以在列表的指定索引位置插入数据\n",
    "name_list.insert(1, \"小妹妹\")\n",
    "\n",
    "print(name_list)\n",
    "\n",
    "# exend 方法可以将其他列表中的完整内容，追加到当前列表的末尾\n",
    "temp_list = [\"孙悟空\", \"猪二哥\", \"沙师弟\"]\n",
    "name_list.extend(temp_list)\n",
    "\n",
    "print(name_list)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4.删除"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['zhangsan', '小妹妹', '李四', '王小二', '孙悟空', '猪二哥', '沙师弟']\n"
     ]
    }
   ],
   "source": [
    "# remove 方法可以从列表中删除指定的数据\n",
    "name_list.remove(\"wangwu\")\n",
    "print(name_list)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['zhangsan', '小妹妹', '李四', '孙悟空', '猪二哥', '沙师弟']\n"
     ]
    }
   ],
   "source": [
    "# pop 方法可以把列表中最后一个元素删除\n",
    "name_list.pop()\n",
    "\n",
    "# pop 方法可以指定要删除元素的索引\n",
    "name_list.pop(3)\n",
    "print(name_list)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沙师弟\n"
     ]
    }
   ],
   "source": [
    "a = name_list.pop()\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "# clear 方法可以清空列表\n",
    "name_list.clear()\n",
    "print(name_list)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## del 关键字"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list assignment index out of range",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mIndexError\u001B[0m                                Traceback (most recent call last)",
      "Input \u001B[1;32mIn [51]\u001B[0m, in \u001B[0;36m<cell line: 1>\u001B[1;34m()\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[38;5;28;01mdel\u001B[39;00m name_list[\u001B[38;5;241m1\u001B[39m]\n\u001B[0;32m      2\u001B[0m \u001B[38;5;28mprint\u001B[39m(name_list)\n",
      "\u001B[1;31mIndexError\u001B[0m: list assignment index out of range"
     ]
    }
   ],
   "source": [
    "del name_list[1]\n",
    "print(name_list)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 列表的数据统计"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "name_list = [\"张三\", \"李四\", \"王五\", \"王小二\", \"张三\"]\n",
    "\n",
    "# len(length长度)函数可以统计列表中元素的总数\n",
    "list_len = len(name_list)\n",
    "print(\"列表中包含%d个元素\" % list_len)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# count 方法可以统计列表中某一个数据出现的次数\n",
    "count = name_list.count(\"张三\")\n",
    "print(\"张三出现了%d次\" % count)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 列表中删除第一次出现的数据，如果数据不存在，程序会报错\n",
    "name_list.remove(\"张三\")\n",
    "\n",
    "print(name_list)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 列表排序"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "name_list = [\"张三\", \"李四\", \"王五\", \"王小二\", \"张三\"]\n",
    "num_list = [6, 8, 41, 1, 10]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.升序"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "name_list.sort()\n",
    "num_list.sort()\n",
    "\n",
    "print(name_list, num_list)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.降序"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "name_list.sort(reverse=True)\n",
    "num_list.sort(reverse=True)\n",
    "\n",
    "print(name_list, num_list)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.逆序（反转）"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "name_list.reverse()\n",
    "num_list.reverse()\n",
    "print(name_list, num_list)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 列表遍历"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "name_list = [\"张三\", \"李四\", \"王五\", \"王小二\"]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 使用迭代遍历列表\n",
    "for i in name_list:\n",
    "    print(i)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 列表生成式"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 列表推导式:用来快速的生成列表。\n",
    "# 好处:代码比较短\n",
    "# 坏处:可读性差\n",
    "# [元素表达式 if(条件) else for 变量 in 迭代对象]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "a = [x for x in range(10)]\n",
    "print(a)\n",
    "\n",
    "b = []\n",
    "for x in range(10):\n",
    "    b.append(x)\n",
    "print(b)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "a = [j for i in range(10) for j in range(i)]\n",
    "print(a)\n",
    "\n",
    "b = []\n",
    "for i in range(10):\n",
    "    for j in range(i):\n",
    "        b.append(j)\n",
    "print(b)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "a = [[col * row for col in range(5)] for row in range(5)]\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "a = [j for x in a for j in x]  #二维转一维\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "a = [x for x in range(10) if x % 2 == 0]\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "a = [x if x % 2 == 0 else x**2 for x in range(10)]\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 应用场景\n",
    "\n",
    "尽管python的列表中可以存储不同类型的数据，但在开发过程中，更多的应用场景是\n",
    "      1. 列表 存储相同类型的数据\n",
    "      2. 通过迭代遍历，再循环体内部，针对列表中的每一项元素，执行相同的操作"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}